from django.shortcuts import render, redirect, get_object_or_404
from django.contrib.auth.decorators import login_required
from django.core.urlresolvers import reverse
from models import *
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

from pprint import pprint

@login_required
def post_spy_report(request):
    data    = {}
    if request.method == 'POST':
        reports         = Report.parse(request.POST['report'], request.user)
        data['reports'] = reports
        #return redirect(reverse('ortools.web.views.shot_spy_report', report.id))
    return render(request, 'post_spy_report.html', data)

@login_required
def show_report(request, id):
    report = get_object_or_404(Report, pk=id)
    return render(request, 'show_report.html', {'report': report})

@login_required
def database(request):
    tmp_reports = Report.objects.all().order_by('-report_date')
    paginator   = Paginator(tmp_reports, 50)

    page = request.GET.get('page')
    try:
        reports = paginator.page(page)
    except PageNotAnInteger:
        reports = paginator.page(1)
    except EmptyPage:
        reports = paginator.page(paginator.num_pages)
    return render(request, 'database.html', {'reports': reports})

@login_required
def show_player(request, id):
    player = get_object_or_404(Player, pk=id)
    return render(request, 'show_player.html', {'player': player, 'planets': player.get_planets()})

@login_required
def show_planet(request, id):
    planet = get_object_or_404(Planet, pk=id)
    return render(request, 'show_planet.html', {'planet': planet, 'reports': planet.get_reports(), 'report': planet.get_last_report()})

@login_required
def map(request):
    planets = Planet.objects.all().order_by('galaxy', 'ss', 'position')
    univers = {}
    for planet in planets:
        if planet.galaxy not in univers:
            univers[planet.galaxy] = {}
        if planet.ss not in univers[planet.galaxy]:
            univers[planet.galaxy][planet.ss] = {}
        univers[planet.galaxy][planet.ss][planet.position] = planet
    return render(request, 'show_map.html', {'univers': univers})

@login_required
def search(request):
    data    = {'results' : []}
    pattern = request.GET['pattern']
    players     = Player.objects.filter(name__icontains=pattern)
    data['results'] = players
    return render(request, 'search_result.html', data)

@login_required
def advanced_search(request):
    return render(request, 'advanced_search.html')


@login_required
def advanced_search_result(request):
    data        = {}
    galaxies    = [int(g) for g in request.POST.getlist('galaxies')]
    results     = []
    res         = request.POST.get('resource')
    if res == u'none':
        results     = Report.objects.filter(planet__galaxy__in=galaxies,planet__ss__gte=int(request.POST.get('ss_start')), planet__ss__lte=int(request.POST.get('ss_end')))
    else:
        tmp_res     = Report.objects.filter(planet__galaxy__in=galaxies,planet__ss__gte=int(request.POST.get('ss_start')), planet__ss__lte=int(request.POST.get('ss_end')))
        for result in tmp_res:
            r   = result.get_resource(res)
            if r >= int(request.POST.get('quantity')):
                result.res = r
                result.res_type = res
                results.append(result)
        t   = {}
        for r in results:
            if r.res not in t: 
                t[r.res] = []
            t[r.res].append(r)
        
        final   = []
        for b in sorted(t, reverse=True):
            for foo in t[b]:
                    final.append(foo)
        results = final
    
    data['reports'] = results
    return render(request, 'advanced_search_result.html', data)
